﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Text;
using System.Xml;
using Utils;
using AlipayClass;

public partial class order_alipay_notify : BasePage
{


    // Methods
    public ArrayList GetRequestPost()
    {
        int index = 0;
        ArrayList list = new ArrayList();
        NameValueCollection form = base.Request.Form;
        string[] allKeys = base.Request.Form.AllKeys;
        for (index = 0; index < allKeys.Length; index++)
        {
            list.Add(allKeys[index] + "=" + base.Request.Form[allKeys[index]]);
        }
        return list;
    }

    private void log(ArrayList sArrary, string result)
    {
        object obj4;
        string path = base.Server.MapPath("~/alipaylog");
        object obj2 = new object();
        object obj3 = new object();
        lock ((obj4 = obj2))
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }
        string str2 = base.Server.MapPath("~/alipaylog/" + DateTime.Now.ToString("yyyyMMdd") + ".config");
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < sArrary.Count; i++)
        {
            builder.Append("&" + sArrary[i]);
        }
        lock ((obj4 = obj3))
        {
            File.AppendAllText(str2, builder.ToString() + result);
        }
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        NameValueCollection system = new SystemConfig().GetSystem();
        ArrayList requestPost = this.GetRequestPost();
        string alipaymid = base.ASSystem.alipaymid;
        string alipaysec = base.ASSystem.Alipaysec;
        string str3 = "utf-8";
        string str4 = "MD5";
        string transport = "https";
        if (requestPost.Count > 0)
        {
            AlipayNotify notify = new AlipayNotify(requestPost, base.Request.Form["notify_id"], alipaymid, alipaysec, str3, str4, transport);
            string responseTxt = notify.ResponseTxt;
            string str7 = base.Request.Form["sign"];
            string mysign = notify.Mysign;
            if (str7 == mysign)
            {
                string str9 = base.Request.Form["trade_no"];
                string orderid = base.Request.Form["out_trade_no"];
                string amt = base.Request.Form["total_fee"];
                string str12 = base.Request.Form["subject"];
                string str13 = base.Request.Form["body"];
                string str14 = base.Request.Form["buyer_email"];
                string str15 = base.Request.Form["trade_status"];
                if (((base.Request.Form["trade_status"] == "TRADE_FINISHED") || (base.Request.Form["trade_status"] == "TRADE_SUCCESS")) || (base.Request.Form["trade_status"] == "WAIT_SELLER_SEND_GOODS"))
                {
                    if ((system["alipay_Manual_SecuredTransactions"] == "1") && (base.Request.Form["trade_status"] == "WAIT_SELLER_SEND_GOODS"))
                    {
                        string url = new AlipayService_Send(alipaymid, str9, string.Empty, string.Empty, "EXPRESS", string.Empty, alipaysec, "utf-8", "md5").Create_url();
                        XmlTextReader reader = new XmlTextReader(url);
                        XmlDocument document = new XmlDocument();
                        document.Load(reader);
                        if (document.SelectSingleNode("/alipay/is_success").InnerText == "T")
                        {
                            base.Response.Write("success");
                            this.log(requestPost, "\r\n通知成功,发货也成功responseTxt=" + responseTxt + ",sign=" + str7 + ",mysign=" + mysign + "\r\n");
                        }
                        else
                        {
                            this.log(requestPost, "\r\n通知成功,发货失败" + url + document.OuterXml + "responseTxt=" + responseTxt + ",sign=" + str7 + ",mysign=" + mysign + "\r\n");
                        }
                    }
                    else
                    {
                        if (orderid.Length > 0)
                        {
                            Maticsoft.BLL.Order.Updateorder(orderid, amt, "alipay");
                        }
                        base.Response.Write("success");
                        this.log(requestPost, "\r\n通知成功,responseTxt=" + responseTxt + ",sign=" + str7 + ",mysign=" + mysign + "\r\n");
                    }
                }
                else
                {
                    base.Response.Write("success");
                }
            }
            else
            {
                this.log(requestPost, "\r\n通知失败,responseTxt=" + responseTxt + ",sign=" + str7 + ",mysign=" + mysign + "\r\n");
                base.Response.Write("fail");
            }
        }
        else
        {
            base.Response.Write("无通知参数");
        }
        base.Response.End();
    }

}